/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Account;
import client.entity.Cv;
import client.entity.Recruitment;
import client.entity.RecruitmentCv;
import client.entity.SaveRecruitment;
import client.entity.Worker;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class ApplyCVBean {

    private String status = "fail";
    private int id;

    public ApplyCVBean() {
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getParam(String param) {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }

    public String applyClickAction() {
        String param = getParam("idJob");
        id = Integer.parseInt(param);
        Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
        if (ac == null) {
            return "login.xhtml?faces-redirect=true";
        } else {
            if (param == null) {
                param = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("idRecruitment");
            } else {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("idRecruitment", param);
            }
            if (ac.getRole() == true) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You are not Worker", "Please Try Again");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } else {

                status = "true";
                return "workerApplyCV.xhtml?accId=" + ac.getId() + "faces-redirect=true";
            }
        }
    }

    public boolean checkCVAndJob(int idCV, int idJob) {
        Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
        if (ac == null) {
            return false;
        } else {
            boolean check=true;
            RecruitmentCv saveRecrui = null;
            SessionFactory factory = HibernateUtil.getSessionFactory();
            Session ss = factory.openSession();
            ss.getTransaction().begin();    
            Worker wk =(Worker) ss.createQuery("from Worker wk where wk.account.id=" + ac.getId()).uniqueResult();
           // List<Cv> cv= ss.createQuery("from Cv cv where cv.worker.id=" + wk.getId()).list();
            List<RecruitmentCv>list =  ss.createQuery("from RecruitmentCv rc where rc.recruitment.id=" + idJob).list();
            for (RecruitmentCv recruitmentCv : list) {
                Cv vc= recruitmentCv.getCv();
                if(vc.getWorker()==wk){
                    check= false;
                }
            }
            ss.getTransaction().commit();
            ss.close();
            if (check) {
                return true;
            } else {
                return false;
            }
        }

    }

    public String aplyAction(int idCV) {
        if (checkCVAndJob(idCV, id)) {
            SessionFactory factory = HibernateUtil.getSessionFactory();
            Session ss = factory.openSession();
            ss.getTransaction().begin();
            RecruitmentCv saveRecrui = new RecruitmentCv();
            Recruitment recruit = (Recruitment) ss.createQuery("from Recruitment rc where rc.id=" + id).uniqueResult();
            Cv cv = (Cv) ss.createQuery("from Cv cv where cv.id=" + idCV).uniqueResult();
            saveRecrui.setCv(cv);
            saveRecrui.setRecruitment(recruit);
            ss.save(saveRecrui);
            ss.getTransaction().commit();
            ss.close();
            return "home.xhtml?faces-redirect=true";
        } else {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message", "You are apply Cv for this job");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        status = "true";


        return null;
    }

    public void applyListener() {
        if (status.equals("fail")) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You are not Worker", "Please Try Again");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
}
